package com.hatic.purchase;

import java.util.Map;

import org.jxstar.dao.DaoParam;
import org.jxstar.service.BusinessObject;
import org.jxstar.util.DateUtil;
import org.jxstar.util.key.KeyCreator;

/**
 * 招标采购单事件处理
 * @author caojiaxin
 * @version 1.0,2011-10-14
 */
public class PurApplyBO extends BusinessObject{

	private static final long serialVersionUID = 1L;

	/**
	 * 修改采购状态为“已公示”
	 * @param applyIds --采购单ID
	 * @return
	 */
	public String onTenderPub(String[] applyIds,String log_user,String add_userid){
		String log_date=DateUtil.getToday();
		
		if(applyIds == null || applyIds.length == 0){
			setMessage("请选择一条采购记录！");
			return _returnFaild;
		}
		for (int i = 0; i < applyIds.length; i++) {
			if(!updatePurStatus(applyIds[i],"2")){
				setMessage("修改采购状态时出错！");
				return _returnFaild;
			}
			if(!createPurLog(applyIds[i], log_date,"已招标公告", log_user, add_userid)){
				setMessage("生成采购日志记录出错！");
				return _returnFaild;
			}
		}
		return _returnSuccess;
	}
	
	/**
	 * 修改采购状态为“已发标”
	 * @param applyIds --采购单ID
	 * @return
	 */
	public String onTenderSale(String[] applyIds,String log_user,String add_userid){
		String log_date=DateUtil.getToday();
		
		if(applyIds == null || applyIds.length == 0){
			setMessage("请选择一条采购记录！");
			return _returnFaild;
		}
		for (int i = 0; i < applyIds.length; i++) {
			if(!updatePurStatus(applyIds[i],"3")){
				setMessage("修改采购状态时出错！");
				return _returnFaild;
			}
			if(!createPurLog(applyIds[i], log_date,"已发售标书", log_user, add_userid)){
				setMessage("生成采购日志记录出错！");
				return _returnFaild;
			}
		}
		return _returnSuccess;
	}
	/**
	 * 提交检查供应商是否有3家以上
	 * @param applyIds
	 * @return
	 */
	public String onAuditCheck(String[] applyIds){
		
		if(applyIds == null || applyIds.length == 0){
			setMessage("请选择一条采购记录！");
			return _returnFaild;
		}
		for (int i = 0; i < applyIds.length; i++) {
			if(!queryprovider(applyIds[i])){
				setMessage("采购申请提交失败：要求供应商至少3家！");
				return _returnFaild;
			}
		}
		return _returnSuccess;
	}
	
	/**
	 * 中标通知提交时反馈中标单位的标志（是否选中）到采购单中
	 * @return
	 */
	public String onAuditUpdate(String[] bidId){
		if(bidId == null || bidId.length == 0){
			setMessage("请选择一条记录！");
			return _returnFaild;
		}
		for (int i = 0; i < bidId.length; i++) {
			if(!updateProvider(bidId[i])){
				setMessage("提交时反馈中标单位出现错误！");
				return _returnFaild;
			}
		}
		
		return _returnSuccess;
	}

	/**
	 * 反馈中标单位的标志到采购单中
	 * @param recordId
	 * @return
	 */
	private boolean updateProvider(String bidId) {
		String selsql="select apply_id,provider_id,provider_name from pur_apply_bid where bid_id=? ";
		DaoParam param=_dao.createParam(selsql);
		param.addStringValue(bidId);
		Map<String, String> map=_dao.queryMap(param);
		_log.showDebug("===========execute sql={"+selsql+"}");
		
		String updatesql="update pur_provider set isbid = '1' where apply_id=? and provider_id=?";
		String updatesql2="update pur_apply set provider_id=? , provider_name=? where apply_id=?";
		_log.showDebug("================execute sql={"+updatesql+"}");
		
		DaoParam uparam=_dao.createParam(updatesql);
		uparam.addStringValue(map.get("apply_id"));
		uparam.addStringValue(map.get("provider_id"));
		
		DaoParam uparam2=_dao.createParam(updatesql2);
		uparam2.addStringValue(map.get("provider_id"));
		uparam2.addStringValue(map.get("provider_name"));
		uparam2.addStringValue(map.get("apply_id"));
		
		if(!_dao.update(uparam) || !_dao.update(uparam2)){
			return false;
		}
		
		return true; 
	}
	
	
	/**
	 * 修改采购状态
	 * @param applyid
	 * @param pur_status
	 * @return
	 */
	private boolean updatePurStatus(String applyid,String pur_status) {
		String sql = "update pur_apply set apply_status = ? where apply_id = ?";
		_log.showDebug("----------applyid=" + applyid + "; sql=" + sql);
		
		DaoParam param = _dao.createParam(sql);
		param.addStringValue(pur_status);
		param.addStringValue(applyid);
		
		return _dao.update(param);
	}
	
	/**
	 * 新增采购日志记录
	 * @param applyid
	 * @param log_date
	 * @param log_content
	 * @param log_user
	 * @param add_userid
	 * @return
	 */
	private boolean createPurLog(String applyid,String log_date,String log_content,String log_user,String add_userid){
		String sql="insert into pur_apply_log(apply_id,log_date,log_content,log_user,log_memo,log_id,"+
		"add_userid,add_date)"+ 
		" values(?,?,?,?,?,?,?,?) ";
		_log.showDebug("=============applyid=" +applyid+";sql"+sql);
		
		String logid=KeyCreator.getInstance().createKey("pur_apply_log");
		
		
		DaoParam param = _dao.createParam(sql);
		param.addStringValue(applyid);
		param.addDateValue(log_date);
		param.addStringValue(log_content);
		param.addStringValue(log_user);
		param.addStringValue("");
		param.addStringValue(logid);
		param.addStringValue(add_userid);
		param.addDateValue(DateUtil.getToday());
		
		return _dao.update(param);
	}
	
	/**
	 * 根据采购单ID查询供应商数量是否少于3
	 * @param applyid
	 * @return
	 */
	private boolean queryprovider(String applyid) {
		String sql = "select count(*) as num from pur_provider where apply_id = ?";
		_log.showDebug("----------applyid=" + applyid + "; sql=" + sql);
		
		DaoParam param = _dao.createParam(sql);
		param.addStringValue(applyid);
		Map<String, String> map = _dao.queryMap(param);
		if(map == null) return false;
		int count = Integer.parseInt(map.get("num")) ;
		
		if(count<3){
			_log.showDebug("============采购申请提交失败，供应商不够3家");
			return false;
		}
		return true;
	}
}
